import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import query from "@dfeidao/atom-nodejs/db/query";
import count from '@dfeidao/atom-nodejs/db/count';
import { systemid, productid } from '../../atom/config';

interface Message {
	plan_list_name: string,
	page_no: number,
	filter: {
		plan_cla_attr_no: string,
		plan_type_no: string
	}
	// cookie: {
	// 	uk: string;
	// 	[key: string]: string
	// };
	// urls: {
	// 	base: string;
	// 	origin: string;
	// 	url: string;
	// };
	// query: {};
	// params: {};
	// headers: {};
	// captcha: string;
}
interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string[];
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

// 表名	plan_list				
// 标题	计划清单				
// 字段名称	字段标题	字段类型	字段长度	是否为空	是否主键
// _id	id	string	50	ⅹ	√
// plan_list_no	计划清单编号	string	50	√	ⅹ
// plan_list_name	计划清单名称	string	50	√	ⅹ
// plan_type_no	计划类型编号	string	50	√	ⅹ
// plan_type_name	计划类型名称	string	50	√	ⅹ
// plan_cla_attr_no	计划分类属性编号	string	50	√	ⅹ
// plan_cla_attr_name	计划分类属性名称	string	50	√	ⅹ
// plan_list_depict	计划清单描述	string	300	√	ⅹ
// plan_list_status	计划清单状态	int	50	√	ⅹ
// productid	产品ID	string	50	√	ⅹ


export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:e:\王举\学习文件\飞道工作文件\测试项目\kh\src\wangju\zj-000001\s001,action_id:' + action_id);
	// const f: { productid: string; plan_cla_attr_no?: string; plan_type_no?: string; $and?: Array<{}>; } = { productid };
	// if (message.filter) {
	// 	const plan_cla_attr_no = message.filter.plan_cla_attr_no;
	// 	const plan_type_no = message.filter.plan_type_no;
	// 	if (plan_type_no && plan_cla_attr_no) {
	// 		f.$and = [{ plan_type_no: { $en: plan_type_no } }, { plan_cla_attr_no: { $en: plan_cla_attr_no } }];
	// 	} else if (plan_cla_attr_no) {
	// 		f.plan_cla_attr_no = { $en: plan_cla_attr_no };
	// 	} else if (plan_type_no) {
	// 		f.plan_type_no = plan_type_no;
	// 	}

	// }
	const { prepare, exec } = query(action_id, session_id, systemid);
	prepare('plan_list', ['plan_list_no', 'plan_list_name', 'plan_cla_attr_no', 'plan_cla_attr_name', 'plan_type_name', 'plan_type_no', 'plan_list_depict', 'plan_list_status'], { productid, plan_list_name: { $like: '%' + message.plan_list_name + '%' }, plan_cla_attr_no: { $like: '%' + message.filter.plan_cla_attr_no + '%' }, plan_type_no: { $like: '%' + message.filter.plan_type_no + '%' } }, 5, message.page_no, [], []);
	prepare('plan_classify', ['plan_cla_attr_no', 'plan_cla_attr_name'], { productid }, 200, 1, [], []);
	const [dt, dt1] = await exec();

	const p = count(action_id, session_id, systemid);
	p.prepare('plan_list', { productid, plan_list_name: { $like: '%' + message.plan_list_name + '%' }, plan_cla_attr_no: { $like: '%' + message.filter.plan_cla_attr_no + '%' }, plan_type_no: { $like: '%' + message.filter.plan_type_no + '%' } });
	const [c] = await p.exec();

	log('Service end path:e:\王举\学习文件\飞道工作文件\测试项目\kh\src\wangju\zj-000001\s001,action_id:' + action_id);
	return {
		data: { dt, dt1, c }
	};
}
